<?php

function formatUid($uid){
    return 'YLTAPP@'.$uid;
}

function createUniqid() {
    return str_replace(".","",uniqid(mt_rand(10000,99999),true));
}

function httpCurl($url,$param){
    $ch = curl_init();  //初始化
    curl_setopt($ch, CURLOPT_URL, $url); //设置url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //不直接输出,以文件流返回
    curl_setopt($ch, CURLOPT_TIMEOUT, 15); //超时
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不需要验证对方的证书
    curl_setopt($ch, CURLOPT_POST, true);   //开启post参数传递
    curl_setopt($ch, CURLOPT_POSTFIELDS, $param); //post数组参数
    $response = curl_exec($ch); //执行,返回header body
    curl_close($ch);//关闭
    return $response;
}

function fileDownLoad($file){
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
}

/*
 * 生成随机码，数字字母
 * $arrLength -生成多少个随机码
 * $strLength -随机码的长度
 * */
function array_random($arrLength,$strLength) {
    $strArr = array(2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','w','x','y','z');
    $codeArr = array();
    while(count($codeArr) < $arrLength){
        shuffle($strArr);
        $rand_keys = array_rand($strArr,$strLength);
        $codeStr = '';
        for ($i = 0; $i < $strLength; $i++) {
            $codeStr .= $strArr[$rand_keys[$i]];
        }
        $codeArr[] = $codeStr;
        $codeArr = array_unique($codeArr);
    }
    return $codeArr;
}

/*数字随机数*/
function number_rand($length=6){
    $numberArr = array(0,1,2,3,4,5,6,7,8,9);
    $randArr = array_rand($numberArr,$length);
    shuffle($randArr);
    return implode('',$randArr);
}

/*
 * 分页函数
 * */
function getPage($totalNum,$listRows,$param=[]){
    $Page = new \Think\Page($totalNum,$listRows,$param);// 实例化分页类 传入总记录数和每页显示的记录数(25)
    $pageShow = $Page->show();// 分页显示输出
    if($pageShow){
        $pageShow = '<ul class="pagination">'.substr($pageShow,5).'</ul>';
    }
    return $pageShow;
}


/*签到日历*/
function signCalendar($signedDate){
    $today = date('Ymd');
    $theMonthStar = strtotime(date('Y-m-01'));
    $theMonthStarStr = date('Y-m-d',$theMonthStar);
    $theMonthEnd = strtotime("$theMonthStarStr +1 month ");
    $res = [];
    for($theMonthStar;$theMonthStar<$theMonthEnd;$theMonthStar += 86400){
        $arr = [];
        $arr['date'] = date('Ymd',$theMonthStar);
        $arr['datej'] = date('j',$theMonthStar);
        $arr['week'] = date('w',$theMonthStar);
        $res[] = $arr;
    }
    $needPaad = $res[0]['week'] - 0;
    $resStr = '<tr>';
    for($i=0;$i<$needPaad;$i++){
        $resStr .= '<td class="no"></td>';
    }
    foreach($res as $val){
        if($val['week'] == 0){
            $resStr .= '<tr>';
        }
        $signedCss = '';
        if($today == $val['date']){
            $signedCss = ' today yes';
        }elseif(in_array($val['date'],$signedDate)){
            $signedCss = ' yes';
        }
        $resStr .= '<td class="date-td '.$signedCss.'" date="'.$val['date'].'">'.$val['datej'].'</td>';
        if($val['week'] == 6){
            $resStr .= '</tr>';
        }
    }
    $monLen = count($res);
    $needPaad = 6 - $res[$monLen-1]['week'];
    $paddStr = '';
    for($i=0;$i<$needPaad;$i++){
        $paddStr .= '<td class="no"></td>';
    }
    if($paddStr){
        $paddStr .= '</tr>';
    }
    $resStr .= $paddStr;
    $res = [];
    $res['resStr'] = $resStr;
    $res['theMonth'] = date('Y年m月');
    $res['theDate'] = $today;
    $theDateSinged = in_array($today,$signedDate) ? 1 : 0;
    $res['theDateSinged'] = $theDateSinged;
    return $res;
}


//检测验证码
function check_verify($code, $id = ''){
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}

//生成订单号
function buildOrderNo($pre){
    $timeStr = date('ymdHis');
    $myPid = str_pad(getmypid(), 5, "0", STR_PAD_LEFT);
    $randNum = rand(10,99);
    $orderNo = $pre.$timeStr.$myPid.$randNum;
    return $orderNo;
}

//时间区域，格式化为每天分钟数
function formatTimeRange($stime,$etime){
    $sdate = date('Y-m-d',$stime);
    $edate = date('Y-m-d',$etime);
    $res = [];
    if($sdate == $edate){
        $arr['date'] = $sdate;
        $arr['sec'] = ceil(($etime-$stime)/60);
        $res[] = $arr;
    }else{
        $ystime = $stime;
        for($stime;$stime<=$etime;$stime+=86400){
            $sdate = date('Y-m-d',$stime);
            $sendDate = $sdate. '23:59:59';
            $sendTime = strtotime($sendDate);
            $arr['date'] = $sdate;
            if($stime == $ystime){
                $arr['sec'] = ceil(($sendTime-$stime)/60);
            }else{
                $sbtime = strtotime($sdate);
                $arr['sec'] = ceil(($sendTime-$sbtime)/60);
            }
            $res[] = $arr;

            if($etime <= $stime+86400){
                $estime = strtotime(date('Y-m-d',$stime+86400));
                $edate = date('Y-m-d',$etime);
                $arr['date'] = $edate;
                $arr['sec'] = ceil(($etime-$estime)/60);
                $res[] = $arr;
            }
        }
    }
    return $res;
}

//清楚字符串bom头
function clearBom($str){
    if (ord($str[0])==239 && ord($str[1])==187 && ord($str[2]) == 191 ) {
        $str = substr($str,3);
    }
    return $str;
}

//格式化星期几
function formatWeek($week){
    $weekArr = ['1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六','7'=>'周日'];
    return $weekArr[$week];
}

//兼容非fpm环境
if (!function_exists("fastcgi_finish_request")) {
    function fastcgi_finish_request()  {
    }
}

//获取用户VIP身份
function getUserVip($userInfo){
    $userVip = $userInfo['vip_level'];
    $userPartner = $userInfo['partner_level'];
    if($userPartner){
        $partnerLevel = C('PARTNER_LEVEL');
        return $partnerLevel[$userPartner];
    }else{
        $vipLevel = C('VIP_LEVEL');
        return $vipLevel[$userVip];
    }
}

/*过滤表情符号*/
function filterEmoji($str){
    $str = preg_replace_callback(
        '/./u',
        function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str);
    return $str;
}
